package enron;

import java.util.ArrayList;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.TickerBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;

public class AdjustGridBehaviour extends TickerBehaviour {
	private static final long serialVersionUID = -2826783655267660696L;

	public AdjustGridBehaviour(Agent a, long period) {
		super(a, period);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onTick() {
		ArrayList<AID> vendors = getListOfPowerGenerators(); 
		if(((EnergyGrid)myAgent).getLoad() > ((EnergyGrid)myAgent).getSupply()) {
			reduceSupply(vendors);
		} else if(((EnergyGrid)myAgent).getLoad() < ((EnergyGrid)myAgent).getSupply()) {
			increaseSupply(vendors);
		}
	}

	private ArrayList<AID> getListOfPowerGenerators() {
		// Update the list of seller agents
	    DFAgentDescription template = new DFAgentDescription(); 
	    ServiceDescription templateSd = new ServiceDescription();
	    ArrayList<AID> vendors = new ArrayList<AID>();
		
	    templateSd.setType("energy-vendor"); 
	    template.addServices(templateSd); 
	    try { 
	      DFAgentDescription[] result = DFService.search(myAgent, template);
	      for(DFAgentDescription buyer : result) { 
	    	  vendors.add(buyer.getName()); 
	      } 
	    } 
	    catch (FIPAException fe) { 
	      fe.printStackTrace(); 
	    }
		return vendors;
	}

	private void increaseSupply(ArrayList<AID> vendors) {
		
	}

	private void reduceSupply(ArrayList<AID> vendors) {
		// TODO Auto-generated method stub
		
	}

}
